import AndroidGoBackHandlerListener from './android-system-goBack'

declare const window: {
  nativeJS: nativeJS
  Vue: any,
  location: any,
}
export type BaseSystemInfo = {
  DEBUG: boolean
  APPLICATION_ID: string
  BUILD_TYPE: string
  VERSION_CODE: number
  VERSION_NAME: string
  APP_NAME: string
  ERROR_PATH: string
  MAIN_URL: string
  WEB_VIEW_CAN_BACK: boolean
  WELCOME_TEXT: string
}

export interface nativeJS {
  /**
   * 判断是否连接成功，用来判断是否支持 nativeJS调用
   * @returns boolean true 是 false 否
   */
  isConnection: () => boolean

  // /**
  //  * 刷新webview
  //  * @returns
  //  */
  // webViewReload: () => void

  /**
   * 发送notify 本地消息推送
   * @param title
   * @param text
   * @returns
   */
  showNotify: (title: string, text: string) => boolean

  /**
   * 获取系统基座信息
   * @returns
   */
  getSystemInfo: () => string //BaseSystemInfo

  /**
   * 播放声音
   * @param url 
   * @returns 
   */
  playPromptSound: (url?: string) => boolean

  /**
   * 取消停止播放声音
   * @returns 
   */
  cancelPromptSound: () => boolean

  /**
  * 手机震动
  * @param isUninterrupted 是否持续震动
  * @param pattern 震动时间间隔配置
  *        当 isUninterrupted=false是，使用 pattern[0]||1000 ;
  *        当 isUninterrupted=true 是，pattern[间隔时长，震动时长，间隔时长，震动时长....];
  * @returns 
  */
  playVibrator: (isUninterrupted: boolean, pattern: number[]) => boolean,

  /**
   * 取消停止震动
   * @returns 
   */
  cancelVibrator: () => boolean,

  /**
   * 下载文件
   * @returns 
   */
  downloadFile: (url: string) => void,
  /**
   * 启动程序
   */
  startApp: (packageName: string) => void,



  /**
   * 退出系统
   * @param isShowConfirmBox  是否显示退出对话框
   * @param isForcibly 是否强制退出
   * @returns 
   */
  exitSystem: (isShowConfirmBox: boolean, isForcibly: boolean) => void,
}

class NativeJS {
  constructor() {
    /**
     * 添加返回支持
     */
    new AndroidGoBackHandlerListener()
    const isCon = this.isConnection()
    if (!isCon) {
      window.nativeJS = {
        isConnection: () => false,
        // webViewReload: () => undefined,
        showNotify: (title: string, text: string) => true,
        getSystemInfo: () => '',
        playPromptSound: (url) => false,
        cancelPromptSound: () => false,
        playVibrator: (isUninterrupted, pattern) => false,
        cancelVibrator: () => false,
        downloadFile: (url) => void (0),
        /**
        * 启动程序
        */
        startApp: (packageName: string) => void (0),
        /**
        * 退出系统
        * @param isShowConfirmBox  是否显示退出对话框
        * @param isForcibly 是否强制退出
        * @returns 
        */
        exitSystem: (isShowConfirmBox: boolean, isForcibly: boolean = false) => void (0),
      }
    }
  }

  /**
   * 判断是否连接成功，用来判断是否支持 nativeJS调用
   * @returns boolean true 是 false 否
   */
  isConnection = (): boolean => {
    return window.nativeJS?.isConnection() || false
  }

  /**
   * 刷新webview
   * @returns
   */
  webViewReload = () => {
    window.location.reload();
  }

  /**
   * 发送notify 本地消息推送
   * @param title
   * @param text
   * @returns
   */
  showNotify = (title: string, text: string): boolean => {
    this.isConnection()
    return window.nativeJS.showNotify(title, text)
  }

  /**
   * 获取系统基座信息
   * @returns
   */
  getSystemInfo = (): BaseSystemInfo | null => {
    this.isConnection()
    try {
      const str = window.nativeJS.getSystemInfo()
      if (!str) {
        return null
      }
      return JSON.parse(
        str.replace(/=/g, '": "').replace(/,\s/g, '", "').replace('{', '{"').replace('}', '"}')
      )
    } catch (error) {
      return null
    }
  }

  /**
   * 发出提示音
   * @returns
   */
  playPromptSound = (url?: string): boolean => {
    this.isConnection()
    return window.nativeJS.playPromptSound(url)
  }

  /**
   * 取消或停止播放
   */
  cancelPromptSound = (): boolean => {
    this.isConnection()
    return window.nativeJS.cancelPromptSound()
  }

  /**
   * 手机震动
   * @param isUninterrupted 是否持续震动
   * @param pattern 震动时间间隔配置
   *        当 isUninterrupted=false是，使用 pattern[0]||1000 ;
   *        当 isUninterrupted=true 是，pattern[震动时长，间隔时长，震动时长，间隔时长....];
   * @returns 
   */
  playVibrator = (isUninterrupted: boolean, pattern: number[]): boolean => {
    this.isConnection()
    return window.nativeJS.playVibrator(isUninterrupted, pattern)
  }

  /**
   * 取消手机震动
   * @returns 
   */
  cancelVibrator = () => {
    this.isConnection()
    return window.nativeJS.cancelVibrator()
  }

  /**
   * 下载文件
   * @returns 
   */
  downloadFile = (url: string) => {
    window.nativeJS.downloadFile(url);
  }

  /**
    * 启动程序
    */
  startApp = (packageName: string) => {
    window.nativeJS.startApp(packageName);
  }


  /**
  * 退出系统
  * @param isShowConfirmBox  是否显示退出对话框
  * @param isForcibly 是否强制退出
  * @returns 
  */
  exitSystem = (isShowConfirmBox: boolean, isForcibly: boolean = false) => {
    window.nativeJS.exitSystem(isShowConfirmBox, isForcibly);
  }



}



import { App } from 'vue'

const install = function (app: App<Element>, options: any = {}) {
  // 也可以声明一个全局属性/函数
  app.config.globalProperties.$native = new NativeJS()
}

/* istanbul ignore if */
if (typeof window !== 'undefined' && window.Vue) {
  install(window.Vue)
}

export default {
  install,
  NativeJS: NativeJS
}
